package com.cloudy.s3.request;

import java.util.Date;
import java.util.Map;

import com.cloudy.exception.AWSAccessException;
import com.cloudy.impl.SuccessHandlerWithFileSave;
import com.cloudy.s3.response.MetadataEntry;

public class GetObject extends S3Request {
	private static final String IF_UNMODIFIED_SINCE = "If-Unmodified-Since";
	private static final String IF_MODIFIED_SINCE = "If-Modified-Since";
	private static final String IF_NONE_MATCH = "If-None-Match";
	private static final String IF_MATCH = "If-Match";
	private static final String RANGE = "Range";
	public GetObject(String bucketName, String objectName, String fullFileName) {
		super(bucketName, "GET");
		super.resource = objectName;
		super.successHandler = new SuccessHandlerWithFileSave(fullFileName);

	}
	public GetObject range(long fromBytes, long toBytes){
		String value = String.valueOf(fromBytes)+"-"+String.valueOf(toBytes);
		super.addHeader(RANGE, "bytes="+value);
		return this;
	}
	public GetObject ifMatch(String eTag){
		super.addHeader(IF_MATCH, eTag);
		return this;
	}

	public GetObject noneMatch(String eTag) {
		super.addHeader(IF_NONE_MATCH, eTag);
		return this;
	}

	public GetObject ifModifiedSince(Date date) {
		String sDate = createDateInAWSFormat(date);
		super.addHeader(IF_MODIFIED_SINCE, sDate);
		return this;
	}
	public GetObject ifUnmodifiedSince(Date date) {
		String sDate = createDateInAWSFormat(date);
		super.addHeader(IF_UNMODIFIED_SINCE, sDate);
		return this;
	}
	
	public Map<String, MetadataEntry> execute() throws AWSAccessException{
		return executeInner().getMetadataEntries();
	}

}
